/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package backoffice;

import ejb.ItemBean;
import ejb.ItemBeanLocal;
import entities.Item;
import entities.User;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.FileUploadEvent;
import web.Constants;

/**
 *
 * @author Alejandro
 */
@ManagedBean
@RequestScoped
public class BackofficeItemManagedBean {

    @EJB
    private ItemBeanLocal itemBean;
    private Item selectedItem;
    private List<Item> filteredItems;
    private List<Item> allItems;
    private List<Item> allActiveItems;

    public BackofficeItemManagedBean() {
        this.itemBean = new ItemBean();
    }

    public ItemBeanLocal getItemBean() {
        return itemBean;
    }

    public void setItemBean(ItemBeanLocal itemBean) {
        this.itemBean = itemBean;
    }

    public Item getSelectedItem() {
        return selectedItem;
    }

    public void setSelectedItem(Item selectedItem) {
        this.selectedItem = selectedItem;
    }

    public List<Item> getAllItems() {
        if (allItems == null) {
            allItems = selectAllItems();
        }
        return allItems;
    }

    public List<Item> getAllActiveItems() {
        if (allActiveItems == null) {
            allActiveItems = selectAllActiveItems();
        }
        return allActiveItems;
    }

    public List<Item> selectAllItems() {
        return this.itemBean.findAll();
    }

    public List<Item> selectAllActiveItems() {
        return this.itemBean.findAllActives();
    }

    public void setAllUsers(List<Item> allItems) {
        this.allItems = allItems;
    }

    public List<Item> getFilteredItems() {
        return filteredItems;
    }

    public void setFilteredItems(List<Item> filteredItems) {
        this.filteredItems = filteredItems;
    }

    public String goToDetails(Item i) {
        if (i != null) {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(Constants.BC_NEXT_VIEW, Constants.BC_ITEM + Constants.SEPARATOR + i.getIdItem());
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(Constants.EXTRA1, i);
        }
        return "UPDATE";

    }

    public String viewItem(Item item) {
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(Constants.NEXT_VIEW, Constants.ITEM + Constants.SEPARATOR + item.getIdItem());
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(Constants.EXTRA1, item);
        return "UPDATE";
    }

    public String saveChanges(Item i) {
        Boolean fatalError = false;
        if (i.getIdCategory() == null) {
            fatalError = true;
        }

        if (i.getRegistrationDate() == null) {
            fatalError = true;
        }

        if (fatalError) {//pongo a null una propiedad crítica para manejar el error en newItem
            i.setName(null);
        }
        return newItem(i);
    }

    public String newItem(Item i) {
        Boolean fatalError = false;



        if (i.getDescription() == null || i.getDescription().isEmpty()) {
            fatalError = true;
        }

        if (i.getName() == null || i.getName().isEmpty()) {
            fatalError = true;
        }

        if (i.getPicture() == null || i.getPicture().isEmpty()) {
            fatalError = true;
        }

        if (i.getPrice() == null || i.getPrice() < 0) {
            fatalError = true;
        }

        if (i.getStatus() == null) {
            fatalError = true;
        }

        if (i.getIdCategory() == null) {
            fatalError = true;
        }

        if (fatalError) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "Error no esperado"));
            return "UPDATE";
        } else {

            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(Constants.BC_NEXT_VIEW, Constants.BC_ITEMS + Constants.SEPARATOR + i.getIdItem());
            itemBean.addItem(i);
            return "UPDATE";
        }

    }

    public String goToNew() {

        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(Constants.BC_NEXT_VIEW, Constants.BC_NEW_ITEM + Constants.SEPARATOR + 1);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(Constants.EXTRA1, new Item());

        return "UPDATE";
    }

    public void handleFileUpload(FileUploadEvent event) {

        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
        selectedItem.setPicture(event.getFile().getFileName());
        Item item = (Item) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(Constants.EXTRA1);
        item.setPicture(event.getFile().getFileName());
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
}
